package wahadlo;

import java.awt.Container;
import java.awt.Dimension;
import java.util.List;
import java.util.Vector;

import javax.swing.JApplet;
import javax.swing.SpringLayout;

import wahadlo.gui.AuthorComponent;
import wahadlo.gui.AxisComponent;
import wahadlo.gui.ControllPanel;
import wahadlo.gui.DataPanel;
import wahadlo.gui.MainFrame;
import wahadlo.gui.SmallVibComponent;
import wahadlo.gui.TypeComponent;
import wahadlo.model.ModelRecipient;
import wahadlo.model.ModelSupplier;

/**
 * Klasa reprezentuje aplet. Odpowiednik klasy {@link MainFrame}.
 * @author Koziołek
 *
 */
public class MainApplet extends JApplet {

	private static final long serialVersionUID = -2525077739551084017L;
	private AxisComponent axisComponent;
	private AuthorComponent authorComponent;
	private TypeComponent typeComponent;
	private SmallVibComponent smallVibComponent;
	private DataPanel dataPanel;
	private ControllPanel controllPanel;
	private Dimension size = new Dimension(900, 475);

	@Override
	public void destroy() {
		super.destroy();
	}

	@Override
	public void init() {
		Container panel = getContentPane();
		setMinimumSize(size);
		setMaximumSize(size);
		SpringLayout layout = new SpringLayout();
		panel.setLayout(layout);

		axisComponent = new AxisComponent();
		authorComponent = new AuthorComponent();
		typeComponent = new TypeComponent();
		smallVibComponent = new SmallVibComponent();
		dataPanel = new DataPanel();

		List<ModelSupplier> modelSuppliers = new Vector<ModelSupplier>(3);
		modelSuppliers.add(typeComponent);
		modelSuppliers.add(smallVibComponent);
		modelSuppliers.add(dataPanel);

		List<ModelRecipient> modelRecipients = new Vector<ModelRecipient>();
		modelRecipients.add(axisComponent);

		controllPanel = new ControllPanel(modelSuppliers, modelRecipients);

		addComponents(panel);

		prepareLayout(layout);

	}

	@Override
	public void start() {
		super.start();
	}

	@Override
	public void stop() {
		super.stop();
	}

	private void addComponents(Container panel) {
		panel.add(axisComponent);
		panel.add(authorComponent);
		panel.add(typeComponent);
		panel.add(smallVibComponent);
		panel.add(controllPanel);
		panel.add(dataPanel);
	}

	private void prepareLayout(SpringLayout layout) {
		layout.putConstraint(SpringLayout.NORTH, axisComponent, 0,
				SpringLayout.NORTH, getContentPane());
		layout.putConstraint(SpringLayout.WEST, axisComponent, 0,
				SpringLayout.WEST, getContentPane());

		layout.putConstraint(SpringLayout.SOUTH, authorComponent, 0,
				SpringLayout.SOUTH, getContentPane());
		layout.putConstraint(SpringLayout.EAST, authorComponent, 0,
				SpringLayout.EAST, getContentPane());

		layout.putConstraint(SpringLayout.SOUTH, typeComponent, -5,
				SpringLayout.SOUTH, getContentPane());
		layout.putConstraint(SpringLayout.WEST, typeComponent, 0,
				SpringLayout.WEST, getContentPane());

		layout.putConstraint(SpringLayout.SOUTH, smallVibComponent, -10,
				SpringLayout.NORTH, authorComponent);
		layout.putConstraint(SpringLayout.EAST, smallVibComponent, -10,
				SpringLayout.EAST, authorComponent);

		layout.putConstraint(SpringLayout.NORTH, controllPanel, 5,
				SpringLayout.NORTH, getContentPane());
		layout.putConstraint(SpringLayout.EAST, controllPanel, -5,
				SpringLayout.EAST, getContentPane());

		layout.putConstraint(SpringLayout.NORTH, dataPanel, 5,
				SpringLayout.SOUTH, controllPanel);
		layout.putConstraint(SpringLayout.EAST, dataPanel, -5,
				SpringLayout.EAST, getContentPane());
	}
}
